package com.shujia.flink.tf;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

public class Demo2FlatMap {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStream<String> linesDS = env.readTextFile("flink/data/words.txt");

        /*
         * 1、定义类实现接口
         */
        DataStream<String> wordsDS = linesDS.flatMap(new FlatMapFun());

        //wordsDS.print();

        /*
         * 2、匿名内部类
         */
        linesDS.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String value, Collector<String> out) throws Exception {
                for (String word : value.split(",")) {
                    out.collect(word);
                }
            }
        });

        /*
         * 3、lambda表达式
         */
        DataStream<String> lambdaDS = linesDS.flatMap((value, out) -> {
            for (String word : value.split(",")) {
                out.collect(word);
            }
        }, Types.STRING);

        lambdaDS.print();


        env.execute();
    }
}
